//------------------------------------------------------------------------------
// <copyright file="ItemHero.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Linq;
using System.Xml.Serialization;

namespace DotBeer.Business.Entities
{
    public class ItemHero : AbstractItem, ICloneable
    {
        #region private fields

        int amount = 1;
        bool amountSpecified, replacementSpecified;
        string type,
               comment;
        Item item;
        ItemHero replacement;
        ItemsCollection itemsCollection;

        #endregion

        #region pubic properties

        public override string Name
        {
            get { return name; }
            set
            {
                this.name = value;
                //SetUnderlyingItem();
                OnPropertyChanged("Name");
                OnPropertyChanged("ImagePath");
            }
        }

        public int Amount
        {
            get { return this.amount; }
            set 
            { 
                this.amount = value;
                OnPropertyChanged("Amount");
                OnPropertyChanged("TotalCost");
            }
        }

        [XmlIgnore]
        public bool AmountSpecified
        {
            get { return this.amountSpecified; }
            set { this.amountSpecified = value; }
        }

        public string Type
        {
            get { return this.type; }
            set 
            {
                this.type = value;
                OnPropertyChanged("Type");
            }
        }

        public string Comment
        {
            get { return this.comment; }
            set 
            {
                this.comment = value;
                OnPropertyChanged("Comment");
            }
        }

        public override string Url
        {
            get { return this.item.Url; }
            set { }
        }

        public override int TotalCost
        {
            get 
            {
                if (this.amount == 0)
                    this.amount = 1;

                return this.item.TotalCost * this.amount; 
            }
        }

        public override int Cost
        {
            get { return this.item.Cost; }
            set { }
        }

        public override int RecipeCost
        {
            get { return this.item.RecipeCost; }
            set { }
        }

        [XmlIgnore]
        public Item Item
        {
            get { return this.item; }
            set { this.item = value; }
        }

        [XmlIgnore]
        public bool ReplacementSpecified
        {
            get { return this.replacementSpecified; }
            set { this.replacementSpecified = true; }
        }

        public ItemHero Replacement
        {
            get 
            {
                if (this.replacement == null)
                {
                    this.replacement = new ItemHero();
                    this.replacement.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(replacement_PropertyChanged);
                }

                return this.replacement; 
            }
            set
            {
                this.replacement = value;
                this.replacement.AmountSpecified = true;
                this.replacementSpecified = value != null;
                OnPropertyChanged("Replacement");
            }
        }

        [XmlIgnore]
        public ItemsCollection ItemsCollection
        {
            get { return this.itemsCollection; }
            set 
            {
                this.itemsCollection = value;

                if (this.itemsCollection != null)
                {
                    this.item = this.itemsCollection.Items.SingleOrDefault(i => i.Name == name);

                    //try
                    //{
                    //    this.item = this.itemsCollection.Items.Single(i => i.Name == name);
                    //}
                    //catch
                    //{
                    //    this.item = new Item();
                    //}

                    OnPropertyChanged("Cost");
                    OnPropertyChanged("TotalCost");
                    OnPropertyChanged("RecipeCost");
                }

                //SetUnderlyingItem();
            }
        }

        #endregion

        #region constructor

        public ItemHero()
        {
            this.recipeCostSpecified = false;
            this.costSpecified = false;
            this.urlSpecified = false;
            this.amountSpecified = false;
            this.replacementSpecified = false;
        }

        #endregion

        #region private methods

        private void SetUnderlyingItem()
        {
            if (this.itemsCollection != null)
            {
                this.item = this.itemsCollection.Items.SingleOrDefault(i => i.Name == name);

                //try
                //{
                //    this.item = this.itemsCollection.Items.Single(i => i.Name == name);
                //}
                //catch
                //{
                //    this.item = new Item();
                //}

                OnPropertyChanged("Cost");
                OnPropertyChanged("TotalCost");
                OnPropertyChanged("RecipeCost");
            }
        }

        #region ICloneable Members

        public ItemHero Clone()
        {
            ItemHero itemHero = new ItemHero()
                {
                    Amount = this.amount,
                    AmountSpecified = this.amountSpecified,
                    Comment = this.Comment,
                    Item = this.item,
                    ItemsCollection = this.itemsCollection,
                    Name = this.name,
                    Type = this.type
                };

            if (this.replacement != null)
                itemHero.Replacement = this.replacement.Clone();

            return itemHero;
        }

        void replacement_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Name")
                this.replacementSpecified = true;
        }

        #endregion

        #region ICloneable Members

        object ICloneable.Clone()
        {
            return Clone();
        }

        #endregion

        #endregion
    }
}
